<template>

  <el-dialog
    :title="isShow"
    :visible.sync="visible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width:80%"
          placeholder="请输入部门名称"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          placeholder="请选择负责人"
          @focus="getUser"
        >
          <el-option
            v-for="item in people"
            :key="item.id"
            :value="item.username"
            :label="item.uaername"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="请输入部门介绍人" />
      </el-form-item>
    </el-form>

    <el-row type="flex" justify="center">
      <el-col :span="6">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-col>

    </el-row>

  </el-dialog>
</template>

<script>
import { addDeptmentList, getDeptmentList, getDeptmentListDail, EditDeptmentList } from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    treeNote: {
      type: Object,
      default: () => ({})
    }

  },
  data() {
    // 部门校验
    const valiName = async(rule, value, callback) => {
      // 校验规则：不能和当前子节点的兄弟节点重合
      // console.log(value, this.treeNote)
      const res = await getDeptmentList()
      const { depts } = res
      let arr = []
      if (this.formData.id) {
        // 编辑部门 排除自己  筛选出兄弟部门
        arr = depts.filter(item => {
          return item.id !== this.formData.id && item.pid === this.treeNote.pid
        })
      } else {
        // 新添部门
        // 筛选出兄弟节点 排除自己
        arr = depts.filter(item => item.pid === this.treeNote.id)
      }
      // console.log(depts)

      // 输入的名字一样
      const repet = arr.some(item => item.name === value)
      repet ? callback(new Error(`该部门下${value}已经存在`)) : callback()
    }
    // 校验编码
    const valiCode = async(rule, value, callback) => {
      // console.log(value, this.treeNote)
      const res = await getDeptmentList()
      const { depts } = res
      const arr = depts.some(item => item.id !== this.treeNote.id && item.code === value)

      arr ? callback(new Error('编码已重复')) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '请输入1到10位数', trigger: 'blur' },
          { validator: valiName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '请输入1到10位数', trigger: 'blur' },
          { validator: valiCode, trigger: 'blur' }
        ]
      },
      people: []
    }
  },
  computed: {
    isShow() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }

  },
  methods: {
    // 获取员工简单列表
    async  getUser() {
      this.people = await getEmployeeSimple()
    },
    // 处理关闭弹框
    handleClose() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: ''// 描述
      }
      this.$emit('update:visible', false)
      this.$refs.form.resetFields()
    },

    // 新增和编辑确定
    async submit() {
      this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑
        await EditDeptmentList()
      } else {
        // 新增
        await addDeptmentList({ ...this.formData, pid: this.treeNote.id })
      }
      const res = await addDeptmentList({ ...this.formData, pid: this.treeNote.id })
      this.$message.success('添加成功')
      console.log(res)
      this.$parent.getDeptment()
      this.handleClose()
    },
    // 获取员工详情
    async  getDeptmentListDail(id) {
      this.formData = await getDeptmentListDail(id)
    }

  }

}

</script>

<style>

</style>
